package com.ruoyi.plan.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.enums.FitnessStatus;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.plan.mapper.StarsPlanFitnessMapper;
import com.ruoyi.plan.domain.StarsPlanFitness;
import com.ruoyi.plan.service.IStarsPlanFitnessService;
import com.ruoyi.common.core.text.Convert;

/**
 * fitnessService业务层处理
 *
 * @author ysz
 * @date 2022 -05-25
 */
@Service
public class StarsPlanFitnessServiceImpl implements IStarsPlanFitnessService 
{
    @Autowired
    private StarsPlanFitnessMapper starsPlanFitnessMapper;

    /**
     * 查询fitness
     * 
     * @param id fitness主键
     * @return fitness
     */
    @Override
    public StarsPlanFitness selectStarsPlanFitnessById(Long id)
    {
        return starsPlanFitnessMapper.selectStarsPlanFitnessById(id);
    }

    /**
     * 查询fitness列表
     * 
     * @return fitness
     */
    @Override
    public Map selectStarsPlanFitnessList()
    {
        //根据完成状态分类装载
        List<Map> res = starsPlanFitnessMapper.selectStarsPlanFitnessList();
        Map<String, ArrayList<Map>> map = new HashMap();
        map.put(FitnessStatus.ALL.getName(),new ArrayList());
        map.put(FitnessStatus.PROCESSED.getName(),new ArrayList());
        map.put(FitnessStatus.FINISHED.getName(),new ArrayList());
        for (Map row : res) {
            if (FitnessStatus.PROCESSED.getCode().equals(row.get("status"))) {
                map.get(FitnessStatus.PROCESSED.getName()).add(row);
            } else if (FitnessStatus.FINISHED.getCode().equals(row.get("status"))) {
                map.get(FitnessStatus.FINISHED.getName()).add(row);
            }
            map.get(FitnessStatus.ALL.getName()).add(row);
        }
        return map;
    }

    /**
     * 根据状态询fitness列表
     *
     * @param status 状态
     * @return fitness
     */
    @Override
    public Map selectStarsPlanFitnessListByStatus(String status)
    {
        //根据完成状态分类装载
        List<Map> res;
        if (status.equals(FitnessStatus.ALL.getName())) {
            res = starsPlanFitnessMapper.selectStarsPlanFitnessList();
        } else {
            //根据枚举类查询状态
            res = starsPlanFitnessMapper.selectStarsPlanFitnessListByStatus(FitnessStatus.valueOf(status.toUpperCase()).getCode());
        }
        Map<String, ArrayList<Map>> map = new HashMap();
        map.put(FitnessStatus.valueOf(status.toUpperCase()).getName(), (ArrayList<Map>) res);
        return map;
    }

    /**
     * 新增fitness
     * 
     * @param starsPlanFitness fitness
     * @return 结果
     */
    @Override
    public int insertStarsPlanFitness(StarsPlanFitness starsPlanFitness)
    {
        starsPlanFitness.setCreateTime(DateUtils.getNowDate());
        //默认进行中
        starsPlanFitness.setStatus(FitnessStatus.PROCESSED.getCode());
        return starsPlanFitnessMapper.insertStarsPlanFitness(starsPlanFitness);
    }

    /**
     * 修改fitness
     * 
     * @param starsPlanFitness fitness
     * @return 结果
     */
    @Override
    public int updateStarsPlanFitness(StarsPlanFitness starsPlanFitness)
    {
        return starsPlanFitnessMapper.updateStarsPlanFitness(starsPlanFitness);
    }

    /**
     * 批量删除fitness
     * 
     * @param ids 需要删除的fitness主键
     * @return 结果
     */
    @Override
    public int deleteStarsPlanFitnessByIds(String ids)
    {
        return starsPlanFitnessMapper.deleteStarsPlanFitnessByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除fitness信息
     * 
     * @param id fitness主键
     * @return 结果
     */
    @Override
    public int deleteStarsPlanFitnessById(Long id)
    {
        return starsPlanFitnessMapper.deleteStarsPlanFitnessById(id);
    }

    /**
     * 修改fitness完成状态
     *
     * @param id fitness主键
     * @return 结果
     */
    @Override
    public int updateFitnessStatus(Long id)
    {
        return starsPlanFitnessMapper.updateFitnessStatus(id);
    }
}
